﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.Model;
using YouHP.BLL;

namespace YouHP.Pages.DiseaseHistory
{
    public class Index : YouHP.Pages.BasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected PatientRecordInfo PrData;
        protected string OccureDate = "";
        protected string ConfirmDate = "";
        protected List<DiseaseTypeInfo> DiseaseTypeList { get; set; }
        protected List<string> mPingYin = new List<string>();

        protected void HandleCommon()
        {
            this.AddLocation("疾病记录", "/DiseaseHistory/Index.aspx");
            this.SetTitle("疾病记录");
            DiseaseTypeList = DiseaseBLL.Instance.GetDiseaseTypeList();
                
      
            for (char c = 'A'; c <= 'Z'; c++)
            {
                mPingYin.Add(c.ToString());
            }
       

#if GENDATA
            UserData.Id = 10;
#endif
            if (UserData.Id <= 0)
            {
                this.AddErrLine("请登录");
                string url = this.GetUrl("/User/Login.aspx");
                ResponseHelper.Redirect(url);
                return;
            }
#if GENDATA
            PrData = new PatientRecordInfo();    
#else
            PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
#endif
            //this.AddLocation("病情记录", "");
            //this.AddLocation("疾病记录", "/DiseaseHistory/Index.aspx");
        }

        protected void HandlePost()
        {
            PatientRecordInfo info = ValidateForm();
            if (info == null) return;
            bool succ = false;
#if GENDATA
            succ = true;
#else
            if (info.Id > 0) succ = PatientBLL.Instance.UpdatePatientRecord(info);
            else succ = PatientBLL.Instance.AddPatientRecord(info);
#endif
            if (succ == false)
            {
                this.AddErrLine("提交失败");
                this.SetShowBackLink(true);
                return;
            }
            else
            {
                this.AddMsgLine("提交成功,你可以进一步完善你的疾病信息：");
            }
        }

        protected void HandleGet()
        {
            if (PrData.Id <= 0)
            {
                int dszid = RequestTools.GetQueryInt("dszid", 0);
                if(dszid > 0)
                {
#if GENDATA
                    DiseaseInfo dszinfo = new DiseaseInfo();
                    dszinfo.Id = dszid;
                    dszinfo.Name = "高血压";
#else
                    DiseaseInfo dszinfo = DiseaseBLL.Instance.GetDiseaseInfo(dszid);
#endif

                    if (dszinfo.Id > 0)
                    {
                        PrData.DiseaseId = dszinfo.Id;
                        PrData.DiseaseName = dszinfo.Name;
                    }
                }
            }

            // patch
            DiseaseInfo dinfo = DiseaseBLL.Instance.GetDiseaseInfo(PrData.DiseaseId);
            PrData.DiseaseName = dinfo.Name;


            if (PrData.Id > 0)
            {
                if (PrData.StartDate.CompareTo(Convert.ToDateTime("1800-01-01")) > 0)
                {
                    OccureDate = PrData.StartDate.ToString("yyyy-MM-dd");
                }
                if (PrData.ConfirmDate.CompareTo(Convert.ToDateTime("1800-01-01")) > 0)
                {
                    ConfirmDate = PrData.ConfirmDate.ToString("yyyy-MM-dd");
                }
            }
        }

        protected PatientRecordInfo ValidateForm()
        {
            PatientRecordInfo info = PrData;
           
            info.DiseaseId = RequestTools.GetInt("dszid", 0);
            if (info.DiseaseId <= 0)
            {
                this.AddErrLine("请选择疾病！");
                return null;
            }

            string date = RequestTools.GetString("odate");
            if (date == "")
            {
                this.AddErrLine("请填写初次发病时间！");
                return null;
            }

            DateTime dd;
            bool succ = DateTime.TryParse(date, out dd);
            if (succ == false)
            {
                this.AddErrLine("无效的初次发病时间：" + dd.ToString("yyyy-MM-dd"));
                return null;
            }
            info.StartDate = dd;

            date = RequestTools.GetFormString("cdate");
            if (date == "")
            {
                this.AddErrLine("请填写确诊时间！");
                return null;
            }

            succ = DateTime.TryParse(date, out dd);
            if (succ == false)
            {
                this.AddErrLine("无效的确诊时间：" + dd.ToString("yyyy-MM-dd"));
                return null;
            }
            info.ConfirmDate = dd;

            //判断时间的先后
            if (DateTime.Compare(info.StartDate, info.ConfirmDate) > 0)
            {
                this.AddErrLine("确诊时间应该比初次发病时间晚！");
                return null;
            }

            //判断与出生日期的先后顺序
            if (DateTime.Compare(info.StartDate, UserData.Birthday) <= 0 )
            {
                this.AddErrLine("无效的初次发病时间：" + info.StartDate.ToString("yyyy-MM-dd"));
                return null;
            }

            //判断与当前日期的先后顺序
            if (DateTime.Compare(info.ConfirmDate, DateTime.Now) >= 0)
            {
                this.AddErrLine("无效的确诊时间：" + info.ConfirmDate.ToString("yyyy-MM-dd"));
                return null;
            }

            info.ConfirmHospital = RequestTools.GetFormString("hospital");
            info.UpdateDate = DateTime.Now;
            info.PatientId = UserData.Id;

            return info;
        }
    }
}
